(function(){
    var _this=this;
    return {
        import:[
            {
                url:"css/plugins/webuploader/webuploader.css"
            },
            {
                url: "js/plugins/webuploader/webuploader.min.js",
            }
        ],
        init:function(){
          this.data.folderId=this.params.folderId;
            this.data.uploadSuccess=this.params.uploadSuccess;
          this.data.fileNumLimit=this.params.fileNumLimit;
        },
        before:function(data){
            return data;
        },
        after:function(){
            var getTask=function(){
                var m=DsModule.getModule("task");
                if(m==null){
                    m=new Module("module/sysFile/task")
                    m.id="task";
                    m.load();
                }
                return m;
            }
            var uploader = WebUploader.create({
                // swf文件路径
                swf: 'js/plugins/webuploader/Uploader.swf',
                server: window.api+'sysFile/upload',
                // 选择文件的按钮。可选。
                // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick:{
                    id:_this.$(".picker"),
                    multiple:this.data.fileNumLimit==1?false:true
                } ,
                // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
                resize: false,
                //文件数量
                fileNumLimit:this.data.fileNumLimit,

            });

            uploader.on('beforeFileQueued',function(file){
                return true;
            });
            //当有文件移除队列的时候
            uploader.on( 'fileDequeued', function( file ) {

            });
            var filesLength=0;
            var filesValidateCount=0;
            var needUpload=0;
            var exec=function(file){
                getTask().call(function (task) {
                    task.add({id: file.id, name: file.name, status: "正在计算md5", progress: 0});
                    uploader.md5File(file)
                        // 及时显示进度
                        .progress(function (percentage) {
                            task.progress(file.id, percentage * 100);
                        })
                        // 完成
                        .then(function (val) {
                            ajaxGet({
                                url: "sysFile/getSysFileByMd5?md5=" + val,
                                success: function (data) {
                                    filesValidateCount++;
                                    if (data.data != null) {
                                        task.status(file.id, "可秒传");
                                        uploader.removeFile( file );
                                        ajaxPost({
                                            url:"sysFile/uploadMd5",
                                            data:{
                                                md5:val,
                                                folderId:_this.data.folderId,
                                                name:file.name
                                            },
                                            success:function(data){
                                                var d=data.data;
                                                data.data=[];
                                                data.data.push(d);
                                                if(_this.data.uploadSuccess!=null){
                                                    _this.data.uploadSuccess(data);
                                                }
                                                task.status(file.id, "秒传成功");
                                            }
                                        })
                                    }else{
                                        needUpload++;
                                    }
                                    if(filesValidateCount==filesLength&&needUpload>0){
                                        uploader.options.formData = {"folderId":_this.data.folderId,"token":window.sysUser.token};
                                        uploader.upload();
                                    }
                                }
                            })
                        });
                });
            }
            // 当有文件被添加进队列的时候
            uploader.on( 'filesQueued', function( files ) {
                filesLength=files.length;
                filesValidateCount=0;
                needUpload=0;
                for(var i=0;i<files.length;i++) {
                    var file = files[i];
                    exec(file);
                }
            });

            // 文件上传过程中创建进度条实时显示。
            uploader.on( 'uploadProgress', function( file, percentage ) {
                getTask().call(function(task){
                    task.status(file.id,"正在上传");
                    task.progress(file.id,percentage*100);
                });
            });


            uploader.on( 'uploadComplete', function( file,response ) {
                uploader.reset();
            });
            uploader.on( 'uploadSuccess', function( file,response ) {
                if(_this.data.uploadSuccess!=null){
                    _this.data.uploadSuccess(response);
                }
                getTask().call(function(task){
                    task.status(file.id,"上传成功");
                });
            });

            uploader.on( 'uploadError', function( file ) {
                getTask().call(function(task){
                    task.status(file.id,"上传出错");
                });
            });

        }
    }
})